<?php

namespace addons\cms\model;

use GuzzleHttp\Psr7\Request;
use think\Model;

class FormField extends Model
{

    // 表名
    protected $name = 'form_field';

    public static function getAllField()
    {
//        var_dump(self::where('form_id',2)->select());
        return self::where('form_id',2)->select();
    }

    /*
     *  生成form表单结构
     * @param array $list 字段数据

     */
    public static function getFormHmtl($tag)
    {
        /* 字段类型
           1   文本框
           2   多行文本
           3   编辑器
           4   文件上传
           5   组图上传
           6   下拉菜单
           7   日期和时间
           8   单选
           9   多选
           10  单图片上传
        */

        $token = Request()->token();

//        var_dump(session('__token__'));

        $form_id = empty($tag['form_id']) ? '' : $tag['form_id'];
        $row = empty($tag['row']) ? '' : $tag['row'];
        $submit = empty($tag['submit']) ? '' : $tag['submit'];

        $str = '';
        $list = self::where('form_id',$form_id)->order('weigh', 'DESC')->limit($row)->select();

        if(is_array($list) && count($list) > 0){
            $str = '<form action="/form_save" name="" method="post">';

            // Token
            $str .= '<input type="hidden" name="__token__" value="'.$token.'" />';
            $str .= '<input type="hidden" name="form_id" value="'.$form_id.'" />';

            foreach ($list as $value){
                switch ($value['show_type']){

                    // 单行文本
                    case 1:
                        $str .= '
                            <div class="form-group">
                                <label for="'.$value['name'].'-'.$value['id'].'">'.$value['title'].'</label>
                                <input type="text" class="form-control" id="'.$value['name'].'-'.$value['id'].'" name="'.$value['name'].'" placeholder="'.$value['notes'].'">
                            </div>
                            ';
                        break;

                    // 多行文本
                    case 2:
                        $str .= '
                            <div class="form-group">
                                <label for="'.$value['name'].'-'.$value['id'].'">'.$value['title'].'</label>
                                <textarea class="form-control" rows="3" id="'.$value['name'].'-'.$value['id'].'" name="'.$value['name'].'" placeholder="'.$value['notes'].'"></textarea>
                            </div>
                            ';
                        break;

                    // 下拉框
                    case 6:
                        if($value['setting'] != ''){
                            $setting = explode('||', $value['setting']);
                            $option_str = '';
                            foreach ($setting as $item){
                                $option = explode('--', $item);

                                if(isset($option[2])){
                                    $selected = ' selected ';
                                }else{
                                    $selected = ' ';
                                }

                                $option_str .= '<option value="'.$option[0].'"'.$selected.'>'.$option[1].'</option>';
                            }
                        }else{
                            $option_str = '<option value="null">NULL</option>';
                        }
                        $str .= '
                            <div class="form-group">
                                <label for="'.$value['name'].'-'.$value['id'].'">'.$value['title'].'</label>
                                <select class="form-control" name="'.$value['name'].'">
                                 '.$option_str.'
                                </select>
                            </div>
                            ';
                        break;

                    // 单选框
                    case 8:
                        if($value['setting'] != ''){
                            $setting = explode('||', $value['setting']);
                            $radios_str = '';
                            foreach ($setting as $item){
                                $option = explode('--', $item);

                                if(isset($option[2])){
                                    $checked = ' checked ';
                                }else{
                                    $checked = ' ';
                                }

                                $radios_str .= '<label class="radio-inline">
                                                  <input type="radio" name="'.$value['name'].'" '.$checked.' value="'.$option[0].'"> '.$option[1].'
                                                </label>';
                            }
                        }else{
                            $radios_str = 'NULL';
                        }
                        $str .= '
                            <div class="form-group">
                            <label for="'.$value['name'].'-'.$value['id'].'">'.$value['title'].'</label>
                                <div class="form-group">'.$radios_str.'</div>
                            </div>';
                        break;

                    // 多选框
                    case 9:
                        if($value['setting'] != ''){
                            $setting = explode('||', $value['setting']);
                            $radios_str = '';
                            foreach ($setting as $item){
                                $option = explode('--', $item);

                                if(isset($option[2])){
                                    $checked = ' checked ';
                                }else{
                                    $checked = ' ';
                                }

                                $radios_str .= '<label class="checkbox-inline">
                                                  <input type="checkbox" name="'.$value['name'].'[]" '.$checked.' value="'.$option[0].'"> '.$option[1].'
                                                </label>';
                            }
                        }else{
                            $radios_str = 'NULL';
                        }
                        $str .= '
                            <div class="form-group">
                            <label for="'.$value['name'].'-'.$value['id'].'">'.$value['title'].'</label>
                                <div class="form-group">'.$radios_str.'</div>
                            </div>';

                        break;
                    default:
                        break;
                }
            }
            $str .= '<button type="submit" class="btn btn-default">'.$submit.'</button>';
            $str .= '</form>';
        }

        if($str == ''){
            $str = 'NULL';
        }

        echo $str;
    }
}
